

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh-CN" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh-CN" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>robot.sdk.unit &mdash; wukong-robot 1.2.0 文档</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
        <script type="text/javascript" src="../../../_static/translations.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> wukong-robot
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">wukong-robot</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">模块代码</a> &raquo;</li>
        
      <li>robot.sdk.unit</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>robot.sdk.unit 源代码</h1><div class="highlight"><pre>
<span></span><span class="c1"># encoding:utf-8</span>
<span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">import</span> <span class="nn">uuid</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">dateutil</span> <span class="k">import</span> <span class="n">parser</span> <span class="k">as</span> <span class="n">dparser</span>
<span class="kn">from</span> <span class="nn">robot</span> <span class="k">import</span> <span class="n">constants</span><span class="p">,</span> <span class="n">logging</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>

<div class="viewcode-block" id="get_token"><a class="viewcode-back" href="../../../robot.sdk.html#robot.sdk.unit.get_token">[文档]</a><span class="k">def</span> <span class="nf">get_token</span><span class="p">(</span><span class="n">api_key</span><span class="p">,</span> <span class="n">secret_key</span><span class="p">):</span>
    <span class="n">cache</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">constants</span><span class="o">.</span><span class="n">TEMP_PATH</span><span class="p">,</span> <span class="s1">&#39;baidustt.ini&#39;</span><span class="p">),</span> <span class="s1">&#39;a+&#39;</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">pms</span> <span class="o">=</span> <span class="n">cache</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">pms</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">time</span> <span class="o">=</span> <span class="n">pms</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">tk</span> <span class="o">=</span> <span class="n">pms</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="c1"># 计算token是否过期 官方说明一个月，这里保守29天</span>
            <span class="n">time</span> <span class="o">=</span> <span class="n">dparser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">time</span><span class="p">)</span>
            <span class="n">endtime</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">endtime</span> <span class="o">-</span> <span class="n">time</span><span class="p">)</span><span class="o">.</span><span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">29</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">tk</span>
    <span class="k">finally</span><span class="p">:</span>
        <span class="n">cache</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="n">URL</span> <span class="o">=</span> <span class="s1">&#39;http://openapi.baidu.com/oauth/2.0/token&#39;</span>
    <span class="n">params</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;grant_type&#39;</span><span class="p">:</span> <span class="s1">&#39;client_credentials&#39;</span><span class="p">,</span>
              <span class="s1">&#39;client_id&#39;</span><span class="p">:</span> <span class="n">api_key</span><span class="p">,</span>
              <span class="s1">&#39;client_secret&#39;</span><span class="p">:</span> <span class="n">secret_key</span><span class="p">}</span>
    <span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">URL</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">r</span><span class="o">.</span><span class="n">raise_for_status</span><span class="p">()</span>
        <span class="n">token</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">json</span><span class="p">()[</span><span class="s1">&#39;access_token&#39;</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">token</span>
    <span class="k">except</span> <span class="n">requests</span><span class="o">.</span><span class="n">exceptions</span><span class="o">.</span><span class="n">HTTPError</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;&#39;</span></div>

<div class="viewcode-block" id="getUnit"><a class="viewcode-back" href="../../../robot.sdk.html#robot.sdk.unit.getUnit">[文档]</a><span class="k">def</span> <span class="nf">getUnit</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">service_id</span><span class="p">,</span> <span class="n">api_key</span><span class="p">,</span> <span class="n">secret_key</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; </span>
<span class="sd">    NLU 解析</span>

<span class="sd">    :param query: 用户的指令字符串</span>
<span class="sd">    :param service_id: UNIT 的 service_id</span>
<span class="sd">    :param api_key: UNIT apk_key</span>
<span class="sd">    :param secret_key: UNIT secret_key</span>
<span class="sd">    :returns: UNIT 解析结果。如果解析失败，返回 None</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">access_token</span> <span class="o">=</span> <span class="n">get_token</span><span class="p">(</span><span class="n">api_key</span><span class="p">,</span> <span class="n">secret_key</span><span class="p">)</span>
    <span class="n">url</span> <span class="o">=</span> <span class="s1">&#39;https://aip.baidubce.com/rpc/2.0/unit/service/chat?access_token=&#39;</span> <span class="o">+</span> <span class="n">access_token</span>
    <span class="n">request</span><span class="o">=</span><span class="p">{</span>
        <span class="s2">&quot;query&quot;</span><span class="p">:</span><span class="n">query</span><span class="p">,</span>
        <span class="s2">&quot;user_id&quot;</span><span class="p">:</span><span class="s2">&quot;888888&quot;</span><span class="p">,</span>
    <span class="p">}</span>
    <span class="n">body</span><span class="o">=</span><span class="p">{</span>
        <span class="s2">&quot;log_id&quot;</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">uuid</span><span class="o">.</span><span class="n">uuid1</span><span class="p">()),</span>
        <span class="s2">&quot;version&quot;</span><span class="p">:</span><span class="s2">&quot;2.0&quot;</span><span class="p">,</span>
        <span class="s2">&quot;service_id&quot;</span><span class="p">:</span> <span class="n">service_id</span><span class="p">,</span>
        <span class="s2">&quot;session_id&quot;</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">uuid</span><span class="o">.</span><span class="n">uuid1</span><span class="p">()),</span>
        <span class="s2">&quot;request&quot;</span><span class="p">:</span><span class="n">request</span>
    <span class="p">}</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">headers</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;Content-Type&#39;</span><span class="p">:</span> <span class="s1">&#39;application/json&#39;</span><span class="p">}</span>
        <span class="n">request</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">json</span><span class="o">=</span><span class="n">body</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">None</span></div>


<div class="viewcode-block" id="getIntent"><a class="viewcode-back" href="../../../robot.sdk.html#robot.sdk.unit.getIntent">[文档]</a><span class="k">def</span> <span class="nf">getIntent</span><span class="p">(</span><span class="n">parsed</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; </span>
<span class="sd">    提取意图</span>

<span class="sd">    :param parsed: UNIT 解析结果</span>
<span class="sd">    :returns: 意图数组</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">parsed</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="s1">&#39;result&#39;</span> <span class="ow">in</span> <span class="n">parsed</span> <span class="ow">and</span> \
       <span class="s1">&#39;response_list&#39;</span> <span class="ow">in</span> <span class="n">parsed</span><span class="p">[</span><span class="s1">&#39;result&#39;</span><span class="p">]:</span>
        <span class="k">return</span> <span class="n">parsed</span><span class="p">[</span><span class="s1">&#39;result&#39;</span><span class="p">][</span><span class="s1">&#39;response_list&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;schema&#39;</span><span class="p">][</span><span class="s1">&#39;intent&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;&#39;</span></div>


<div class="viewcode-block" id="hasIntent"><a class="viewcode-back" href="../../../robot.sdk.html#robot.sdk.unit.hasIntent">[文档]</a><span class="k">def</span> <span class="nf">hasIntent</span><span class="p">(</span><span class="n">parsed</span><span class="p">,</span> <span class="n">intent</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; </span>
<span class="sd">    判断是否包含某个意图</span>

<span class="sd">    :param parsed: UNIT 解析结果</span>
<span class="sd">    :param intent: 意图的名称</span>
<span class="sd">    :returns: True: 包含; False: 不包含</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">parsed</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="s1">&#39;result&#39;</span> <span class="ow">in</span> <span class="n">parsed</span> <span class="ow">and</span> \
       <span class="s1">&#39;response_list&#39;</span> <span class="ow">in</span> <span class="n">parsed</span><span class="p">[</span><span class="s1">&#39;result&#39;</span><span class="p">]:</span>
        <span class="n">response_list</span> <span class="o">=</span> <span class="n">parsed</span><span class="p">[</span><span class="s1">&#39;result&#39;</span><span class="p">][</span><span class="s1">&#39;response_list&#39;</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">response</span> <span class="ow">in</span> <span class="n">response_list</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">response</span><span class="p">[</span><span class="s1">&#39;schema&#39;</span><span class="p">][</span><span class="s1">&#39;intent&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="n">intent</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="kc">False</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="getSlots"><a class="viewcode-back" href="../../../robot.sdk.html#robot.sdk.unit.getSlots">[文档]</a><span class="k">def</span> <span class="nf">getSlots</span><span class="p">(</span><span class="n">parsed</span><span class="p">,</span> <span class="n">intent</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; </span>
<span class="sd">    提取某个意图的所有词槽</span>
<span class="sd">    </span>
<span class="sd">    :param parsed: UNIT 解析结果</span>
<span class="sd">    :param intent: 意图的名称</span>
<span class="sd">    :returns: 词槽列表。你可以通过 name 属性筛选词槽，</span>
<span class="sd">再通过 normalized_word 属性取出相应的值</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">parsed</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="s1">&#39;result&#39;</span> <span class="ow">in</span> <span class="n">parsed</span> <span class="ow">and</span> \
       <span class="s1">&#39;response_list&#39;</span> <span class="ow">in</span> <span class="n">parsed</span><span class="p">[</span><span class="s1">&#39;result&#39;</span><span class="p">]:</span>
        <span class="n">response_list</span> <span class="o">=</span> <span class="n">parsed</span><span class="p">[</span><span class="s1">&#39;result&#39;</span><span class="p">][</span><span class="s1">&#39;response_list&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">intent</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">parsed</span><span class="p">[</span><span class="s1">&#39;result&#39;</span><span class="p">][</span><span class="s1">&#39;response_list&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;schema&#39;</span><span class="p">][</span><span class="s1">&#39;slots&#39;</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">response</span> <span class="ow">in</span> <span class="n">response_list</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">response</span><span class="p">[</span><span class="s1">&#39;schema&#39;</span><span class="p">][</span><span class="s1">&#39;intent&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="n">intent</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">response</span><span class="p">[</span><span class="s1">&#39;schema&#39;</span><span class="p">][</span><span class="s1">&#39;slots&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="p">[]</span></div>

<div class="viewcode-block" id="getSlotWords"><a class="viewcode-back" href="../../../robot.sdk.html#robot.sdk.unit.getSlotWords">[文档]</a><span class="k">def</span> <span class="nf">getSlotWords</span><span class="p">(</span><span class="n">parsed</span><span class="p">,</span> <span class="n">intent</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; </span>
<span class="sd">    找出命中某个词槽的内容</span>
<span class="sd">    </span>
<span class="sd">    :param parsed: UNIT 解析结果</span>
<span class="sd">    :param intent: 意图的名称</span>
<span class="sd">    :param name: 词槽名</span>
<span class="sd">    :returns: 命中该词槽的值的列表。</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">slots</span> <span class="o">=</span> <span class="n">getSlots</span><span class="p">(</span><span class="n">parsed</span><span class="p">,</span> <span class="n">intent</span><span class="p">)</span>
    <span class="n">words</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">slot</span> <span class="ow">in</span> <span class="n">slots</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">slot</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="n">name</span><span class="p">:</span>
            <span class="n">words</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">slot</span><span class="p">[</span><span class="s1">&#39;normalized_word&#39;</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">words</span></div>

<div class="viewcode-block" id="getSay"><a class="viewcode-back" href="../../../robot.sdk.html#robot.sdk.unit.getSay">[文档]</a><span class="k">def</span> <span class="nf">getSay</span><span class="p">(</span><span class="n">parsed</span><span class="p">,</span> <span class="n">intent</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    提取 UNIT 的回复文本</span>

<span class="sd">    :param parsed: UNIT 解析结果</span>
<span class="sd">    :param intent: 意图的名称</span>
<span class="sd">    :returns: UNIT 的回复文本</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">parsed</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="s1">&#39;result&#39;</span> <span class="ow">in</span> <span class="n">parsed</span> <span class="ow">and</span> \
       <span class="s1">&#39;response_list&#39;</span> <span class="ow">in</span> <span class="n">parsed</span><span class="p">[</span><span class="s1">&#39;result&#39;</span><span class="p">]:</span>
        <span class="n">response_list</span> <span class="o">=</span> <span class="n">parsed</span><span class="p">[</span><span class="s1">&#39;result&#39;</span><span class="p">][</span><span class="s1">&#39;response_list&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">intent</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">response_list</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;action_list&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;say&#39;</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">response</span> <span class="ow">in</span> <span class="n">response_list</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">response</span><span class="p">[</span><span class="s1">&#39;schema&#39;</span><span class="p">][</span><span class="s1">&#39;intent&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="n">intent</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">response</span><span class="p">[</span><span class="s1">&#39;action_list&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;say&#39;</span><span class="p">]</span>
        <span class="k">return</span> <span class="s1">&#39;&#39;</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;&#39;</span></div>


<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">parsed</span> <span class="o">=</span> <span class="n">getUnit</span><span class="p">(</span><span class="s1">&#39;今天的天气&#39;</span><span class="p">,</span> <span class="s2">&quot;S13442&quot;</span><span class="p">,</span> <span class="s1">&#39;w5v7gUV3iPGsGntcM84PtOOM&#39;</span><span class="p">,</span> <span class="s1">&#39;KffXwW6E1alcGplcabcNs63Li6GvvnfL&#39;</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">parsed</span><span class="p">)</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, wzpan

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>